package com.seedog.mq.distributed;

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;

/**
 * 节点类，代表分布式系统中的一个节点
 */
public class Node implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 节点唯一标识
    private final String nodeId;
    // 节点主机名或IP地址
    private final String host;
    // 节点端口号
    private final int port;
    // 节点角色（MASTER, SLAVE, STANDBY等）
    private NodeRole role;
    // 节点状态（ACTIVE, INACTIVE, SYNCHRONIZING等）
    private NodeStatus status;
    // 节点优先级
    private int priority;
    // 最后心跳时间
    private long lastHeartbeatTime;
    
    public Node(String host, int port) {
        this.nodeId = UUID.randomUUID().toString();
        this.host = host;
        this.port = port;
        this.role = NodeRole.SLAVE;
        this.status = NodeStatus.INACTIVE;
        this.priority = 1;
        this.lastHeartbeatTime = System.currentTimeMillis();
    }
    
    public Node(String nodeId, String host, int port) {
        this.nodeId = nodeId;
        this.host = host;
        this.port = port;
        this.role = NodeRole.SLAVE;
        this.status = NodeStatus.INACTIVE;
        this.priority = 1;
        this.lastHeartbeatTime = System.currentTimeMillis();
    }
    
    // Getters and Setters
    public String getNodeId() {
        return nodeId;
    }
    
    public String getHost() {
        return host;
    }
    
    public int getPort() {
        return port;
    }
    
    public NodeRole getRole() {
        return role;
    }
    
    public void setRole(NodeRole role) {
        this.role = role;
    }
    
    public NodeStatus getStatus() {
        return status;
    }
    
    public void setStatus(NodeStatus status) {
        this.status = status;
    }
    
    public int getPriority() {
        return priority;
    }
    
    public void setPriority(int priority) {
        this.priority = priority;
    }
    
    public long getLastHeartbeatTime() {
        return lastHeartbeatTime;
    }
    
    public void updateHeartbeat() {
        this.lastHeartbeatTime = System.currentTimeMillis();
    }
    
    // 获取节点的地址字符串
    public String getAddress() {
        return host + ":" + port;
    }
    
    // 检查节点是否活跃
    public boolean isAlive(long timeout) {
        return System.currentTimeMillis() - lastHeartbeatTime < timeout;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node node = (Node) o;
        return Objects.equals(nodeId, node.nodeId);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(nodeId);
    }
    
    @Override
    public String toString() {
        return "Node{" +
                "nodeId='" + nodeId + '\'' +
                ", address='" + getAddress() + '\'' +
                ", role=" + role +
                ", status=" + status +
                '}';
    }
    
    /**
     * 节点角色枚举
     */
    public enum NodeRole {
        MASTER,  // 主节点
        SLAVE,   // 从节点
        STANDBY  // 备用节点
    }
    
    /**
     * 节点状态枚举
     */
    public enum NodeStatus {
        ACTIVE,          // 活跃状态
        INACTIVE,        // 非活跃状态
        SYNCHRONIZING,   // 同步中
        FAILED,          // 失败状态
        SHUTTING_DOWN    // 正在关闭
    }
}